//
//  USMomentsPhotoCell.m
//  us
//
//  Created by karry on 2025/6/2.
//

#import "USMomentsPhotoCell.h"
#import <QuickLookThumbnailing/QuickLookThumbnailing.h>

@interface USMomentsPhotoCell ()

@property (nonatomic,strong)UIImageView *imageView;

@property (nonatomic, strong)UIView *liveTagView;

@property (nonatomic, strong)UIImageView *videoImageView;

@property (nonatomic, strong) UIImageView *deleteImageView;

@end

@implementation USMomentsPhotoCell

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupView];
    }
    return self;
}
- (void)setIsEdit:(BOOL)isEdit {
    _isEdit = isEdit;
    
    self.imageView.layer.cornerRadius = isEdit ? 0 : 14;
    self.deleteImageView.hidden = isEdit;
    self.liveTagView.hidden = isEdit;
    self.videoImageView.hidden = isEdit;
}

- (void)setIsAdd:(BOOL)isAdd {
    _isAdd = isAdd;
    
    self.deleteImageView.hidden = isAdd;

    if (isAdd) {
        self.imageView.image = [UIImage imageNamed:@"moments_add"];
        self.imageView.backgroundColor = UIColor.mi_3_color;
        self.liveTagView.hidden = YES;
        self.videoImageView.hidden = YES;
    }else{
        
        self.liveTagView.hidden = self.media.type != USMediaTypeLivePhoto;
        self.videoImageView.hidden = self.media.type != USMediaTypeVideo;
    }
}

- (void)setMedia:(USMediaResult *)media {
    _media = media;
    
    if (media.type == USMediaTypeVideo) {
        NSURL *url = [NSURL fileURLWithPath:media.filePath];

        QLThumbnailGenerationRequest *request = [[QLThumbnailGenerationRequest alloc]initWithFileAtURL:url size:CGSizeMake(200, 200) scale:UIScreen.mainScreen.scale representationTypes:QLThumbnailGenerationRequestRepresentationTypeThumbnail];
        
        [[QLThumbnailGenerator sharedGenerator]generateBestRepresentationForRequest:request completionHandler:^(QLThumbnailRepresentation * _Nullable thumbnail, NSError * _Nullable error) {
           
            dispatch_async(dispatch_get_main_queue(), ^{
                if (error) {
                    self.imageView.image = [UIImage us_placeHolderImage];
                    return;
                }
                self.imageView.image = thumbnail.UIImage;
            });
            
        }];
    }else {
        self.imageView.image = [UIImage imageWithContentsOfFile:media.filePath];
    }
    
    
    self.liveTagView.hidden = media.type != USMediaTypeLivePhoto;
    self.videoImageView.hidden = media.type != USMediaTypeVideo;


}
- (void)setPhotoModel:(USPhotoModel *)photoModel {
    _photoModel = photoModel;
    
    if (photoModel.type == USMediaTypeImage) {
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:photoModel.cosUrl.photoThumbnail]];
    }else if (photoModel.type == USMediaTypeVideo) {
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:photoModel.cosUrl.videoThumbnail]];
    }else if (photoModel.type == USMediaTypeLivePhoto) {
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:photoModel.cosUrl.photoThumbnail]];
    }
    
    self.liveTagView.hidden = photoModel.type != USMediaTypeLivePhoto;
    self.videoImageView.hidden = photoModel.type != USMediaTypeVideo;

}
 
- (void)deleteAction {
    if (self.deleteBlock) {
        self.deleteBlock();
    }
}

- (void)setupView{
    
    self.imageView = [[UIImageView alloc]initWithImage:nil];
    self.imageView.backgroundColor = [[UIColor jk_randomMacaronColor] colorWithAlphaComponent:.5];
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    self.imageView.layer.cornerRadius = 14;
    self.imageView.layer.masksToBounds = YES;
    [self.contentView addSubview:self.imageView];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_offset(4);
        make.right.bottom.mas_offset(-4);
    }];
    self.contentView.clipsToBounds = YES;
 
    
    // 添加 live 标签
    [self.contentView addSubview:self.liveTagView];
    [self.liveTagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(4);
        make.bottom.mas_equalTo(-4);
        make.height.mas_equalTo(20);
    }];
    
    self.deleteImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"moments_delete"]];
    [self.contentView addSubview:self.deleteImageView];
    [self.deleteImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.mas_equalTo(0);
        make.width.height.mas_equalTo(22);
    }];
    self.deleteImageView.userInteractionEnabled = YES;
    @weakify(self)
    [self.deleteImageView jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
     @strongify(self)
        [self deleteAction];
    }];
    
    
    UIImageSymbolConfiguration *config = [UIImageSymbolConfiguration configurationWithPointSize:30 weight:UIImageSymbolWeightRegular];
    UIImage *image = [UIImage systemImageNamed:@"play.circle" withConfiguration:config];
    self.videoImageView = [[UIImageView alloc] initWithImage:image];
    self.videoImageView.hidden = YES;
    [self.contentView addSubview:self.videoImageView];
    [self.videoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
    }];
}
- (UIView *)liveTagView {
    if (_liveTagView == nil) {
        _liveTagView = [[UIView alloc] init];
        _liveTagView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
        _liveTagView.layer.cornerRadius = 4;
        _liveTagView.layer.masksToBounds = YES;

        UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage systemImageNamed:@"livephoto"]];
        iconView.contentMode = UIViewContentModeScaleAspectFit;
        [_liveTagView addSubview:iconView];
        
        UILabel *label = [[UILabel alloc] init];
        label.text = @"LIVE";
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:11];
        [_liveTagView addSubview:label];

      
        [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(6);
            make.centerY.equalTo(_liveTagView);
            make.width.height.mas_equalTo(16);
        }];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(iconView.mas_right).offset(4);
            make.right.mas_equalTo(-6);
            make.centerY.equalTo(_liveTagView);
        }];
    }
    return _liveTagView;
}

@end
